package com.smm.web.vmc.controller.User;

import com.smm.web.vmc.domain.Alam;
import com.smm.web.vmc.domain.User;
import com.smm.web.vmc.domain.Warehouse;
import com.smm.web.vmc.dto.AlamQueryDto;
import com.smm.web.vmc.dto.JsonReturn;
import com.smm.web.vmc.service.AlamService;
import com.smm.web.vmc.service.ReceiptService;
import com.smm.web.vmc.service.UserService;
import com.smm.web.vmc.service.WarehouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zhenghao on 2016/9/5.
 * 报警记录控制类
 */
@Slf4j
@Controller
@RequestMapping("/home/alam")
public class  AlamController {

    @Resource
    private AlamService alamService;

    @Resource
    private WarehouseService warehouseService;

    @Resource
    private ReceiptService receiptService;


    @RequestMapping("/list.do")
    public ModelAndView list(HttpServletRequest request){

        //从session中获取用户信息
        User user = (User) request.getSession().getAttribute(UserService.USER_SESSION_KEY);

        //获取当前用户的所有报警记录列表， 按报警时间倒叙排列
        List<Alam> alams = alamService.getAlamListByUser(user.getId());

        //获取仓库列表，用于搜索条件
        List<Warehouse> warehouses = warehouseService.getAll();

        //统计品类列表，用于搜索条件
        List<String> breeds = receiptService.countBreed();

        ModelAndView mv = new ModelAndView("alamList");

        mv.addObject("alams",alams);
        mv.addObject("warehouses",warehouses);
        mv.addObject("breeds",breeds);

        return mv;
    }


    /**
     * 按条件查询
     * @param queryDto
     * @param request
     * @return
     */
    @RequestMapping("/query.do")
    public ModelAndView query(AlamQueryDto queryDto,HttpServletRequest request){

        //按查询条件过滤仓单
        //从session中获取用户信息
        User user = (User) request.getSession().getAttribute(UserService.USER_SESSION_KEY);

        log.info("---queryDto :"+queryDto.toString());


        //根据查询条件获取报警记录
        List<Alam> alams = alamService.queryByQueryAndUser(queryDto, user.getId());


        ModelAndView mv = new ModelAndView("alamList");

        //获取仓库列表，用于搜索条件
        List<Warehouse> warehouses = warehouseService.getAll();

        //统计品类列表，用于搜索条件
        List<String> breeds = receiptService.countBreed();

        mv.addObject("warehouses",warehouses);
        mv.addObject("breeds",breeds);
        mv.addObject("alams",alams);

        return mv;
    }


    /**
     * 查看详情
     * @param alamId
     * @return
     */
    @RequestMapping("/info.do")
    @ResponseBody
    public JsonReturn info(int alamId){

        Alam alam = alamService.getOneById(alamId);

        return new JsonReturn(1,"",alam);
    }

}
